home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include "exinterfmotif.h"
- #include <math.h>
- #include <string.h>
- #include "exbookglo.h"
- #include "exglobals.h"
-
- extern struct indexlist *(find_keyword_int() );
- extern struct indexlist *(add_keyword() );
- extern struct indexlist *find_keyword();
- extern struct icntmpltstruct *new_icn();
- extern struct grptmpltstruct *new_grp();
- extern struct pagestruct *new_pg();
- extern void readicon_i();
- extern struct filestruct *add_file();
- extern struct icntmpltstruct **duplicate_icons;
- extern int dup_icn_cnt, max_dup;
- extern Boolean append;
-
- float ascii_version = .01;
-
- int linecount;
-
- int find_endofline(char *bigstring)
- {
- int count;
-
- count = 0;
- while (bigstring[count] != '\n')
- count++;
- return(count);
- }
-
- int skip_comments(int filendx, char *thefile)
- {
- int len;
-
- while (thefile[filendx] == '#')
- {
- len = find_endofline(&(thefile[filendx]));
- filendx = filendx+len +1;
- linecount++;
- }
- return(filendx);
- }
-
- void do_format_error(int linecount)
- {
- sprintf(msgstring,"Format Problem line %d ascii file %s \n",
- linecount,FileName);
- DialogType = 1;
- popup_Message();
- }
-
- void check_ending_blanks(char *extract, int len)
- {
- int i;
-
- i = len -1;
- while (i > 0 && (extract[i] == ' ' || extract[i] == '\t') )
- {
- extract[i] = '\0';
- i--;
- }
- }
- void fix_grp_ok(struct grptmpltstruct *grpptr)
- {
- Boolean big_ok;
- struct iconstruct *tmpicon;
-
- tmpicon = grpptr->firstpage->fronticons;
- big_ok = FALSE;
- while (tmpicon != NULL)
- {
- if (tmpicon->ok)
- big_ok = TRUE;
- tmpicon = tmpicon->nexticon;
- }
-
- grpptr->ok = big_ok;
- }
-
-
- Boolean read_ascii(char *thefile, long totalbytes)
- {
- int len;
- int cmp_rslt;
- int filendx = 0;
- Boolean FILEERR;
- char *firstline;
- float version_read;
- char *extract;
- int extractlen;
- struct icntmpltstruct *tmpicn;
- struct grptmpltstruct *tmpgrp, *prevgrp;
- Boolean Books, Demos;
- struct filelist *tmpfile;
- struct pagestruct *tmppg;
- struct wordlist *tmpkey;
- struct indexlist *tmpname;
- struct iconstruct *tmpicon;
- float tmpr, tmpg, tmpb;
- int num_conv;
- short x, y;
- int SIDE;
- struct grptmpltstruct *setupgrp;
- struct wordlist *wordptr;
- struct grpliststruct *glist;
- int i;
-
- extract = (char *) malloc(80);
- extractlen = 80;
- tmpicn = NULL;
- setupgrp = NULL;
- tmpgrp = NULL;
- prevgrp = lastgroup;
- tmpfile = NULL;
- tmppg = NULL;
- tmpkey = NULL;
- tmpicon = NULL;
- linecount = 0;
- firstline = (char *)malloc(28 * sizeof (char) );
- strcpy(firstline,"Demobook ASCII file version");
- /* is this a demobook ascii file? */
- filendx = skip_comments(filendx, thefile);
- if (cmp_rslt = strncasecmp(firstline, &(thefile[filendx]), strlen(firstline)) != 0)
- {
- FILEERR = TRUE;
- sprintf(msgstring, "\nError: %s is not a demobook file.", FileName);
- DialogType = 1;
- popup_Message();
- }
- else
- {
- FILEERR = FALSE;
- filendx = filendx + strlen(firstline) + 1;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- version_read = (float)atof(extract);
- filendx = filendx+len +1;
- linecount++;
- if (version_read != ascii_version)
- {
- sprintf(msgstring, "\nError: bad ascii file version %f. Should be %f \n",version_read, ascii_version);
- DialogType = 1;
- popup_Message();
- }
- else
- {
- dup_icn_cnt = 0;
- max_dup = 32;
- duplicate_icons = (struct icntmpltstruct **) malloc
- ( max_dup* sizeof(struct icntmpltstruct *));
- while (filendx < totalbytes)
- {
- if (thefile[filendx] == '#')
- {
- len = find_endofline(&(thefile[filendx]));
- filendx = filendx+len +1;
- linecount++;
- }
- /* read demos */
- else if ((cmp_rslt = strncasecmp("Demo List", &(thefile[filendx]), 9)) == 0)
- {
- len = find_endofline(&(thefile[filendx]));
- filendx = filendx+len +1;
- linecount++;
- Books = FALSE;
- Demos = TRUE;
- }
- /* read books */
- else if ((cmp_rslt = strncasecmp("Book List", &(thefile[filendx]), 9)) == 0)
- {
- if (lasticon!= NULL)
- do_iconfiles(lasticon);
- len = find_endofline(&(thefile[filendx]));
- filendx = filendx+len +1;
- linecount++;
- Books = TRUE;
- Demos = FALSE;
- }
- else
- {
- linecount++;
- /* when we get here we can have blank lines; or Name, Command,
- Icon Files, Files, or Keyword lines for Demos; or Name, Color,
- Keyword, or Demo lines for Books. Books and Demos lines can
- start with either a tab, or a series of blanks. */
- len = find_endofline(&(thefile[filendx]));
- if (len >= 5) /* assume short lines are blank */
- {
- if (thefile[filendx] == '\t')
- filendx++;
- else
- while (thefile[filendx] == ' ')
- filendx++;
- if ((cmp_rslt = strncasecmp("Name", &(thefile[filendx]), 4)) == 0)
- {
- filendx = filendx + 5;
- tmpkey = NULL;
- if (Demos)
- {
- if (lasticon!= NULL)
- do_iconfiles(lasticon);
- tmpfile = NULL;
- numberoficons++;
- tmpicn = new_icn();
- if (firsticon == NULL)
- firsticon = tmpicn;
- else
- lasticon->nexticntmplt = tmpicn;
- lasticon = tmpicn;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- check_ending_blanks(extract, len);
- tmpicn->nameptr = add_keyword(extract);
- if (tmpicn->nameptr->icon != NULL) /* ?? duplicate */
- {
- if (append) /*found dup while appending a file*/
- {
- duplicate_icons[dup_icn_cnt] = tmpicn;
- dup_icn_cnt++;
- if (dup_icn_cnt == max_dup)
- {
- max_dup = max_dup + 32;
- duplicate_icons = (struct icntmpltstruct **) realloc
- ( (void *) duplicate_icons, max_dup* sizeof(struct icntmpltstruct *));
- }
- }
- else /* found dup while reading file */
- {
- sprintf(msgstring, "There are two demos with the name <%s>\nin this file. This will cause problems in adding demos to books. \n", extract );
- DialogType = 1;
- popup_Message();
- }
- }
- else
- tmpicn->nameptr->icon = tmpicn;
- }
- else if (Books)
- {
- /* check to see if previous group had demos in it */
- if (tmpgrp != NULL && (tmpgrp->firstpage == NULL || tmpgrp->firstpage->frontnumicons == 0))
- {
- sprintf(msgstring, " Deleting empty book %s \n",tmpgrp->nameptr->string);
- DialogType = 1;
- popup_Message();
- DeleteGroupFlag = TRUE;
- deletegroup(tmpgrp);
- DeleteGroupFlag = FALSE;
- }
- else if (tmpgrp != NULL)
- {
- #ifdef DEBUG
- printf("\nfixing up group %s \n", tmpgrp->nameptr->string);
- #endif
- fix_grp_ok(tmpgrp);
- }
- if (setupgrp != NULL)
- {
- fix_icon_pointers(setupgrp);
- group_setup(NULL, NULL,setupgrp);
- }
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- check_ending_blanks(extract, len);
- tmpname = find_keyword(extract);
- if (tmpname == NULL || tmpname->group == NULL)
- {
- prevgrp = tmpgrp;
- tmpicon = NULL;
- tmpkey = NULL;
- tmppg = NULL;
- SIDE = FRONT;
- x = 0;
- y = 3;
- numberofgroups++;
- numberofbooks = numberofgroups;
- tmpgrp = new_grp();
- if (firstgroup == NULL)
- firstgroup = tmpgrp;
- else
- lastgroup -> nextgrp = tmpgrp;
- lastgroup = tmpgrp;
- tmpgrp->prevgrp = prevgrp;
- tmpgrp->numpages = 1;
- tmpgrp->nameptr = add_keyword(extract);
- tmpgrp->nameptr->group = tmpgrp;
- setupgrp = NULL;
- }
- else /* group already exists */
- {
- tmpgrp = tmpname->group;
- tmppg = tmpgrp->lastpage;
- if (tmppg->backicons == NULL)
- {
- tmpicon = tmppg->fronticons;
- SIDE = FRONT;
- }
- else
- {
- tmpicon = tmppg->backicons;
- SIDE = BACK;
- }
- while (tmpicon->nexticon != NULL)
- {
- x = tmpicon->xposition_ndx;
- y = tmpicon->yposition_ndx;
- tmpicon = tmpicon->nexticon;
- }
- setupgrp = tmpgrp;
- tmpkey = tmpgrp->keywords;
- if (tmpkey != NULL)
- while (tmpkey->next != NULL)
- tmpkey = tmpkey->next;
- }
- }
- }
- else if ((cmp_rslt = strncasecmp("Command", &(thefile[filendx]), 7)) == 0)
- {
- if (Demos)
- {
- filendx = filendx + 8;
- len = find_endofline(&(thefile[filendx]));
- if (len > extractlen)
- {
- free(extract);
- extract = (char *) malloc( len + 10);
- if (extract == NULL)
- printf(" extract = NULL\n");
- extractlen = len + 10;
- }
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- set_start_string(tmpicn, extract);
- }
- else if (Books)
- {
- do_format_error(linecount);
- }
- }
- else if ((cmp_rslt = strncasecmp("AltCommand", &(thefile[filendx]), 10)) == 0)
- {
- if (Demos)
- {
- filendx = filendx + 11;
- len = find_endofline(&(thefile[filendx]));
- if (len > extractlen)
- {
- extract = (char *) realloc ( (void *) extract, len + 10);
- extractlen = len + 10;
- }
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- set_alt_command(tmpicn, extract);
- }
- else if (Books)
- {
- do_format_error(linecount);
- }
- }
- else if ((cmp_rslt = strncasecmp("Geom Icon File", &(thefile[filendx]), 14)) == 0)
- {
- if (Demos)
- {
- filendx = filendx + 15;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- tmpicn->geofile = add_file(extract);
- }
- else if (Books)
- {
- do_format_error(linecount);
- }
- }
- else if ((cmp_rslt = strncasecmp("Img Icon File", &(thefile[filendx]), 13)) == 0)
- {
- if (Demos)
- {
- filendx = filendx + 14;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- tmpicn->imgfile = add_file(extract);
- }
- else if (Books)
- {
- do_format_error(linecount);
- }
- }
- else if ((cmp_rslt = strncasecmp("Help", &(thefile[filendx]), 4)) == 0)
- {
- filendx = filendx + 5;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- if (Demos)
- tmpicn->helpfile = add_file(extract);
- else if (Books)
- tmpgrp->helpfile = add_file(extract);
- }
- else if ((cmp_rslt = strncasecmp("File", &(thefile[filendx]), 4)) == 0)
- {
- if (Demos)
- {
- filendx = filendx + 5;
- if (tmpfile == NULL)
- {
- tmpicn->addtlfiles = (struct filelist *)malloc(sizeof(struct filelist) );
- tmpfile = tmpicn->addtlfiles;
- }
- else
- {
- tmpfile->next = (struct filelist *)malloc(sizeof(struct filelist) );
- tmpfile = tmpfile->next;
- }
- tmpfile->next = NULL;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- tmpfile->file = add_file(extract);
- if (tmpicn->ok && !tmpfile->file->ok)
- {
- tmpicn->ok = FALSE;
- }
- }
- else if (Books)
- {
- do_format_error(linecount);
- }
- }
- else if ((cmp_rslt = strncasecmp("Keyword", &(thefile[filendx]), 7)) == 0)
- {
- filendx = filendx + 8;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- check_ending_blanks(extract, len);
- if (Demos)
- {
- (tmpicn->numkey)++;
- if (tmpkey == NULL)
- {
- tmpicn->keywords = (struct wordlist *)malloc(sizeof(struct wordlist) );
- tmpkey = tmpicn->keywords;
- }
- else
- {
- tmpkey->next = (struct wordlist *)malloc(sizeof(struct wordlist) );
- tmpkey = tmpkey->next;
- }
- tmpkey->next = NULL;
- tmpkey->indexptr = add_keyword(extract);
- if (tmpkey->indexptr != NULL)
- index_add_demo(tmpkey->indexptr, tmpicn);
- }
- else if (Books)
- {
- /* check for duplicate keywords */
- tmpname = find_keyword(extract);
- wordptr = tmpgrp->keywords;
- while (wordptr != NULL && wordptr->indexptr != tmpname)
- wordptr = wordptr->next;
- if (wordptr == NULL) /* no dup found */
- {
- (tmpgrp->numkey)++;
- if (tmpkey == NULL)
- {
- tmpgrp->keywords = (struct wordlist *)malloc(sizeof(struct wordlist) );
- tmpkey = tmpgrp->keywords;
- }
- else
- {
- tmpkey->next = (struct wordlist *)malloc(sizeof(struct wordlist) );
- tmpkey = tmpkey->next;
- }
- tmpkey->next = NULL;
- tmpkey->indexptr = add_keyword(extract);
- if (tmpkey->indexptr != NULL)
- index_add_group(tmpkey->indexptr, tmpgrp);
- }
- }
- }
- else if ((cmp_rslt = strncasecmp("Color", &(thefile[filendx]), 5)) == 0)
- {
- if (Demos)
- {
- do_format_error(linecount);
- }
- else if (Books)
- {
- filendx = filendx + 6;
- if ((num_conv = sscanf(&(thefile[filendx])," %f %f %f",
- &tmpr, &tmpg, &tmpb)) == 3)
- {
- tmpgrp->covercolor[0] = tmpr;
- tmpgrp->covercolor[1] = tmpg;
- tmpgrp->covercolor[2] = tmpb;
- }
- }
- }
- else if ((cmp_rslt = strncasecmp("Demo", &(thefile[filendx]), 4)) == 0)
- {
- if (Demos)
- {
- do_format_error(linecount);
- }
- else if (Books)
- {
- filendx = filendx + 5;
- len = find_endofline(&(thefile[filendx]));
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- check_ending_blanks(extract, len);
- tmpname = find_keyword(extract);
- if (tmpname == NULL)
- {
- sprintf(msgstring,"Demo %s not found in database\nline %d ascii file %s \n",
- extract, linecount,FileName);
- DialogType = 1;
- popup_Message();
- }
- else
- {
- if (tmpicon == NULL)
- {
- tmpicon = (struct iconstruct *)malloc(sizeof(struct iconstruct) );
- if (tmppg == NULL)
- {
- tmppg = new_pg();
- tmpgrp->firstpage = tmppg;
- tmpgrp->lastpage = tmppg;
- tmppg->fronticons = tmpicon;
- }
- }
- else
- {
- tmpicon->nexticon = (struct iconstruct *)malloc(sizeof(struct iconstruct) );
- tmpicon = tmpicon->nexticon;
- }
- if (SIDE == FRONT)
- {
- tmppg->frontnumicons = tmppg->frontnumicons + 1;
- if (tmppg->frontnumicons > ICONLIMIT)
- setupgrp = tmpgrp;
- }
- else
- {
- tmppg->backnumicons = tmppg->backnumicons + 1;
- if (tmppg->backnumicons > ICONLIMIT)
- setupgrp = tmpgrp;
- }
- tmpicon->iconnum = 0;
- tmpicon->xposition_ndx = x;
- tmpicon->yposition_ndx = y;
- x++;
- if (x > 4)
- {
- x = 0;
- y--;
- if (y < 0)
- {
- x = 0;
- y = 3;
- }
- }
- tmpicon->nexticon = NULL;
- if (tmpname != NULL)
- {
- /* have to see if there is a duplicate icon with this name */
- if (dup_icn_cnt > 0)
- {
- i = 0;
- while (i < dup_icn_cnt && duplicate_icons[i]->nameptr != tmpname)
- {
- i++;
- }
- if (i < dup_icn_cnt && duplicate_icons[i]->nameptr == tmpname)
- tmpicon->iconptr = duplicate_icons[i];
- else
- tmpicon->iconptr = tmpname->icon;
- }
- else
- tmpicon->iconptr = tmpname->icon;
- glist = tmpicon->iconptr->grps;
- tmpicon->iconptr->grps = (struct grpliststruct *)
- malloc(sizeof(struct grpliststruct) );
- tmpicon->iconptr->grps->prev = NULL;
- tmpicon->iconptr->grps->next = glist;
- if (glist != NULL)
- glist->prev = tmpicon->iconptr->grps;
- tmpicon->iconptr->grps->grpptr = tmpgrp;
- tmpicon->ok = tmpicon->iconptr->ok;
- (tmpicon->iconptr->occurences)++;
- }
- }
- }
- }
- else
- {
- strncpy(extract, &(thefile[filendx]),len);
- extract[len] = '\0';
- sprintf(msgstring, "Error with line %d in file %s \n <%s>",
- linecount,FileName, extract);
- DialogType = 1;
- popup_Message();
- }
- }
- len = find_endofline(&(thefile[filendx]));
- filendx = filendx+len +1;
- }
- }
- }
- }
- /* check to see if previous group had demos in it */
- if (tmpgrp != NULL && (tmpgrp->firstpage == NULL || tmpgrp->firstpage->frontnumicons == 0))
- {
- sprintf(msgstring, " Deleting empty book %s \n",tmpgrp->nameptr->string);
- DialogType = 1;
- popup_Message();
- DeleteGroupFlag = TRUE;
- deletegroup(tmpgrp);
- DeleteGroupFlag = FALSE;
- }
- else if (tmpgrp != NULL)
- {
- fix_grp_ok(tmpgrp);
- }
- if (lastgroup != NULL && lastgroup->firstpage->frontnumicons > ICONLIMIT)
- {
- group_setup(NULL, NULL,lastgroup);
- }
- initbookpos();
- check_index();
- ask_user_duplicate_icons(dup_icn_cnt);
- return(!FILEERR);
- }
-
- write_ascii(char *FileName)
- {
- FILE *ofp;
- struct icntmpltstruct *tmpicn;
- struct filelist *tmpfile;
- struct grptmpltstruct *tmpgrp;
- struct pagestruct *tmppg;
- struct wordlist *tmpkey;
- struct iconstruct *tmpicon;
-
- if ( (ofp = fopen(FileName,"w")) == NULL)
- {
- sprintf(msgstring, "Could not open file \n%s\nfor output ...\nFile will not be written.",FileName);
- DialogType = 1;
- popup_Message();
- }
- else
- {
- fprintf(ofp, "Demobook ASCII file version %f \n",ascii_version);
- fprintf(ofp, "#\n");
- fprintf(ofp, "#\n");
- fprintf(ofp, "Demo List\n");
- tmpicn = firsticon;
- while (tmpicn != NULL)
- {
- fprintf(ofp, "\tName %s\n", tmpicn->nameptr->string);
- fprintf(ofp, "\tCommand %s\n", tmpicn->startstring);
- if (tmpicn->alt_command != NULL)
- fprintf(ofp, "\tAltCommand %s\n", tmpicn->alt_command);
- if (tmpicn->geofile != NULL)
- fprintf(ofp, "\tGeom Icon File %s\n", tmpicn->geofile->name);
- if (tmpicn->imgfile != NULL)
- fprintf(ofp, "\tImg Icon File %s\n", tmpicn->imgfile->name);
- tmpfile = tmpicn->addtlfiles;
- while (tmpfile != NULL)
- {
- fprintf(ofp, "\tFile %s\n", tmpfile->file->name);
- tmpfile = tmpfile->next;
- }
- tmpkey = tmpicn->keywords;
- while (tmpkey != NULL)
- {
- fprintf(ofp, "\tKeyword %s\n", tmpkey->indexptr->string);
- tmpkey = tmpkey->next;
- }
- fprintf(ofp, "#\n");
- tmpicn = tmpicn->nexticntmplt;
- }
- fprintf(ofp, "#\n");
- fprintf(ofp, "#\n");
- fprintf(ofp, "Book List\n");
- tmpgrp = firstgroup;
- while (tmpgrp != NULL)
- {
- fprintf(ofp, "\tName %s\n", tmpgrp->nameptr->string);
- fprintf(ofp, "\tColor %f %f %f\n", tmpgrp->covercolor[0],
- tmpgrp->covercolor[1], tmpgrp->covercolor[2]);
- tmpkey = tmpgrp->keywords;
- while (tmpkey != NULL)
- {
- fprintf(ofp, "\tKeyword %s\n", tmpkey->indexptr->string);
- tmpkey = tmpkey->next;
- }
- tmppg = tmpgrp->firstpage;
- tmpicon = tmppg->fronticons;
- while (tmpicon != NULL)
- {
- fprintf(ofp, "\tDemo %s\n", tmpicon->iconptr->nameptr->string);
- tmpicon = tmpicon->nexticon;
- }
- fprintf(ofp, "#\n");
- tmpgrp = tmpgrp->nextgrp;
- }
- fclose(ofp);
- sprintf(msgstring, "\nSaved the ascii Demobook file\n%s\n", FileName);
- popup_Message();
- }
- }
-
-